<%@ page import="rcfirewall.conexaoBD,java.sql.*,javax.sql.*"%>
<%@ page import="java.io.*,java.util.*,javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Softpar Firewall</title>
</head>
<body>
	<%
		/*
		 To delete the rule we need it's unique ID from the database. Having that we go into the database and get the parameters to create
		 the command that will delete it.
		 */

		String ID = request.getParameter("id");
		conexaoBD bd = new conexaoBD();
		ResultSet rs = bd.montaDropDown2("select * from RULES where id='" + ID + "'");
		String ruleDelete;
		String direction;
		String source;
		String destiny;
		String srcPort;
		String dstPort;
		String protocol;
		String comment;
		String action;
		int type;
		String name;

		if (rs.next() == true) {
			ruleDelete = rs.getString(3);
			direction = rs.getString(4);
			protocol = rs.getString(5);
			source = rs.getString(6);
			srcPort = rs.getString(7);
			destiny = rs.getString(8);
			dstPort = rs.getString(9);
			comment = rs.getString(10);
			action = rs.getString(11);
			type = rs.getInt(12);
			name = rs.getString(13);

			//We check to see if it's a normal rule or a Full Access Rule.
			switch (type) {

			//Case 1 - Normal Rule. 
			case 1:
				//Executing the command to delete the rule.   	
				try {

					String[] commands = new String[] {"iptables", "-D", direction, "-p", protocol, "-s", source, "--sport", srcPort, "-d"
							, destiny, "--dport", dstPort, "-m", "comment", "--comment", comment + ". ID da Regra " + ID, "-j", action };

					commands = new String[] {"iptables", "-D", direction, "-p", protocol, "-s", source, "--sport", srcPort, "-d"
							, destiny, "--dport", dstPort, "-m", "comment", "--comment", comment + ". ID da Regra " + ID, "-j", action };

					Process child = Runtime.getRuntime().exec(commands);

				} catch (IOException e) {
				}
				//Deleting the rule from the database.
				bd.insertBD("delete from RULES where id =" + ID);

				//Just printing the rule to the user and saying it was deleted.
				out.println(ruleDelete);
				out.println("<br></br>");
				out.println("A regra acima foi deletada!");
				break;
			
			//Case 2 -> Full Access Rule.	
			case 2:

				try {

					String[] commands = new String[] {"iptables", "-D", "FORWARD" ,"-s", source, "-d", "0/0",
				    		"-m", "comment", "--comment", "Acesso Full " + name + ". ID da Regra " + ID, "-j", "ACCEPT"};

					commands = new String[] {"iptables", "-D", "FORWARD" ,"-s", source, "-d", "0/0",
				    		"-m", "comment", "--comment", "Acesso Full " + name + ". ID da Regra " + ID, "-j", "ACCEPT"};

					Process child = Runtime.getRuntime().exec(commands);

				} catch (IOException e) {
				}
				//Deleting the rule from the database.
				bd.insertBD("delete from RULES where id =" + ID);

				//Just printing the rule to the user and saying it was deleted.
				out.println(ruleDelete);
				out.println("<br></br>");
				out.println("A regra acima foi deletada!");
				break;
			}
 
 		} else {//in case we don't find the ID in the database we tell the user.
			out.println("ID não encontrado");
		}
	%>
</body>
</html>